package com.anhuanjia.tools.eao.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.anhuanjia.tools.eao.bean.EaoPublicProject;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.utility.StringUtil;

@Alias(value = "publicProject")
public class PublicProjectUI extends UIFacade {

	/**
	 * 新增/更新公用工程
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdatePublicProject() {
		String publicProjectList = $V("PublicProjectList");
		if (StringUtil.isNotEmpty(publicProjectList)) {
			Transaction transaction = new Transaction();
			List<Map<String, Object>> array = JSON.parseObject(publicProjectList,
					new TypeReference<List<Map<String, Object>>>() {
					});
			List<String> ppids = new ArrayList<>();
			String project_id = null;
			for (Map<String, Object> list : array) {
				System.out.println(list);
				EaoPublicProject publicProject = new EaoPublicProject();
				publicProject.setValue(list);
				project_id = publicProject.getProjectId();
				String id = publicProject.getPpId();
				if (StringUtils.isEmpty(id)) {
					// 添加
					publicProject.setPpId(UUID.randomUUID().toString());
					transaction.insert(publicProject);
					ppids.add(publicProject.getPpId());
				} else {
					// 更新
					ppids.add(id);
					DataTable dt = new Q(" select * from eao_public_project where PpId = ?", id).fetch();
					if (dt.getRowCount() != 0) {
						EaoPublicProject entity = new EaoPublicProject();
						entity.setValue(dt.get(0));
						entity.setValue(list);
						transaction.update(entity);
					}
				}
			}
			DataTable dt = new Q(" select * from eao_public_project where ProjectId = ?", project_id).fetch();
			for (DataRow dataRow : dt) {
				EaoPublicProject publicProject = new EaoPublicProject();
				publicProject.setValue(dataRow);
				Boolean del = true;
				for (String ppid : ppids) {
					if (publicProject.getPpId().equals(ppid)) {
						del = false;
					}
				}
				if (del) {
					transaction.delete(publicProject);
				}
			}
			if (transaction.commit()) {
				s("操作成功");
			} else {
				f("操作失败");
			}
		} else {
			this.Response.put("data", "无操作记录");
		}
	}


	/**
	 * 根据id查询公用工程
	 */
	@Priv(loginType = LoginType.Member)
	public void findOne() {
		String id = $V("Id");
		if (StringUtil.isNotEmpty(id)) {
			DataTable dt = new Q(" select * from eao_public_project where PpId = ?", id).fetch();
			if (dt.getRowCount() != 0) {
				EaoPublicProject publicProject = new EaoPublicProject();
				publicProject.setValue(dt.get(0));
				this.Response.put("data", publicProject);
			}
		}
	}

	/**
	 * 根据项目id查询公用工程
	 */
	@Priv(loginType = LoginType.Member)
	public void findByProjectId() {
		String id = $V("Id");
		List<EaoPublicProject> list = new ArrayList<>();
		DataTable dataTable = new Q(" select * from eao_public_project where ProjectId = ?", id).fetch();
		for (DataRow dataRow : dataTable) {
			System.out.println(dataTable.get(0));
			EaoPublicProject entity = new EaoPublicProject();
			entity.setValue(dataRow);
			list.add(entity);
			this.Response.put("data", list);
		}
	}

	/**
	 * 公用工程删除
	 */
	@Priv(loginType = LoginType.Member)
	public void delete() {
		String id = $V("Id");
		if (StringUtil.isNotEmpty(id)) {
			DataTable dataTable = new Q(" select * from eao_public_project where PpId = ?", id).fetch();
			if (dataTable.getRowCount() != 0) {
				EaoPublicProject entity = new EaoPublicProject();
				entity.setValue(dataTable.get(0));
				Transaction transaction = new Transaction();
				transaction.delete(entity);
				transaction.commit();
				if (transaction.commit()) {
					s("公用工程删除成功");
				} else {
					f("公用工程删除失败");
				}
			}
		}
	}
}
